package org.whh.automatic.ui;

import org.whh.automatic.model.Step;
import org.whh.automatic.ui.dialog.CaptureCoordsDialog;
import org.whh.automatic.ui.dialog.DialogManager;
import org.whh.automatic.ui.dialog.ShortcutRecorderDialog;
import org.whh.automatic.util.MouseActionUtil;
import org.whh.automatic.util.StepDialogUIUtil;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.io.Serial;

/**
 * 步骤编辑对话框
 * <p>
 * 该对话框用于创建和编辑自动化任务的步骤，支持三种操作类型：
 * 1. 快捷键操作：
 * - 录制键盘快捷键组合
 * - 显示已录制的快捷键
 * 2. 坐标点击操作：
 * - 设置点击坐标（手动输入或捕获）
 * - 选择点击类型（单击/双击）
 * - 支持坐标测试
 * 3. 复制粘贴操作：
 * - 固定值：直接输入要粘贴的文本
 * - 动态值：设置动态值的名称，运行时获取
 * <p>
 * 界面采用现代化的设计风格：
 * - 渐变色标题栏
 * - 圆角按钮
 * - 悬停效果
 * - 分区边框
 * - 合理的间距和布局
 */
public class StepDialog extends JDialog {

    @Serial
    private static final long serialVersionUID = 1L;

    /** 界面颜色常量 */
    /**
     * 标题栏渐变起始颜色
     */
    private static final Color HEADER_GRADIENT_START = new Color(65, 105, 225);
    /**
     * 标题栏渐变结束颜色
     */
    private static final Color HEADER_GRADIENT_END = new Color(100, 149, 237);
    /**
     * 面板背景色
     */
    private static final Color PANEL_BACKGROUND = new Color(245, 245, 250);
    /**
     * 边框颜色
     */
    private static final Color BORDER_COLOR = new Color(210, 210, 210);
    /**
     * 按钮背景色
     */
    private static final Color BUTTON_BACKGROUND = new Color(70, 130, 180);
    /**
     * 按钮悬停颜色
     */
    private static final Color BUTTON_HOVER = new Color(30, 144, 255);
    /**
     * 按钮文字颜色
     */
    private static final Color BUTTON_TEXT = Color.WHITE;

    /**
     * 步骤名称输入框
     */
    private JTextField nameField;

    /**
     * 快捷键输入框（只读，通过录制填充）
     */
    private JTextField shortcutField;

    /**
     * 录制快捷键按钮
     */
    private JButton recordShortcutButton;

    /**
     * 坐标X输入框
     */
    private JSpinner xSpinner;

    /**
     * 坐标Y输入框
     */
    private JSpinner ySpinner;

    /**
     * 单击选择按钮
     */
    private JRadioButton singleClickRadio;

    /**
     * 双击选择按钮
     */
    private JRadioButton doubleClickRadio;

    /**
     * 粘贴文本输入区域
     */
    private JTextArea pasteValueField;

    /**
     * 动态值名称输入框
     */
    private JTextField dynamicNameField;

    /**
     * 等待时间输入框
     */
    private JSpinner waitTimeSpinner;

    /**
     * 快捷键操作单选按钮
     */
    private JRadioButton shortcutRadio;

    /**
     * 坐标点击操作单选按钮
     */
    private JRadioButton coordinateRadio;

    /**
     * 复制粘贴操作单选按钮
     */
    private JRadioButton pasteRadio;

    /**
     * 坐标设置面板
     */
    private JPanel coordinatePanel;

    /**
     * 快捷键设置面板
     */
    private JPanel shortcutPanel;

    /**
     * 粘贴设置面板
     */
    private JPanel pastePanel;

    /**
     * 固定值输入面板
     */
    private JPanel fixedValuePanel;

    /**
     * 动态值设置面板
     */
    private JPanel dynamicValuePanel;

    /**
     * 用户是否确认保存
     */
    private boolean confirmed = false;

    /**
     * 当前正在编辑的步骤对象
     */
    private Step currentStep;

    /**
     * 构造函数 - 创建新步骤
     *
     * @param parent 父窗口
     * @param title  对话框标题
     */
    public StepDialog(JDialog parent, String title) {
        super(parent, title, true);
        this.currentStep = new Step();

        initComponents();

        setSize(600, 720);
        setLocationRelativeTo(parent);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }

    /**
     * 构造函数 - 编辑现有步骤
     *
     * @param parent 父窗口
     * @param title  对话框标题
     * @param step   要编辑的步骤对象
     */
    public StepDialog(JDialog parent, String title, Step step) {
        super(parent, title, true);
        this.currentStep = step;

        initComponents();

        // 首先设置所有面板的可见性为false
        shortcutPanel.setVisible(false);
        coordinatePanel.setVisible(false);
        pastePanel.setVisible(false);

        // 填充已有数据
        nameField.setText(step.getName());

        if (step.getActionType() == Step.ActionType.SHORTCUT) {
            shortcutRadio.setSelected(true);
            shortcutField.setText(step.getShortcut());
            shortcutPanel.setVisible(true);
            enableShortcutPanel(true);
            enableCoordinatePanel(false);
            enablePastePanel(false);
        } else if (step.getActionType() == Step.ActionType.COORDINATE) {
            coordinateRadio.setSelected(true);
            xSpinner.setValue(step.getCoordinate().x);
            ySpinner.setValue(step.getCoordinate().y);

            // 设置点击类型
            if (step.isDoubleClick()) {
                doubleClickRadio.setSelected(true);
            } else {
                singleClickRadio.setSelected(true);
            }

            coordinatePanel.setVisible(true);
            enableShortcutPanel(false);
            enableCoordinatePanel(true);
            enablePastePanel(false);
        } else if (step.getActionType() == Step.ActionType.PASTE) {
            pasteRadio.setSelected(true);
            pastePanel.setVisible(true);
            enableShortcutPanel(false);
            enableCoordinatePanel(false);
            enablePastePanel(true);

            // 设置值类型单选按钮
            JPanel pasteTypePanel = (JPanel) pastePanel.getComponent(0);
            Component[] components = pasteTypePanel.getComponents();
            for (Component c : components) {
                if (c instanceof JRadioButton) {
                    JRadioButton radio = (JRadioButton) c;
                    if (step.getPasteValueType() == Step.PasteValueType.FIXED && radio.getText().equals("固定值")) {
                        radio.setSelected(true);
                        pasteValueField.setText(step.getPasteFixedValue());
                        showFixedValuePanel(true);
                        showDynamicValuePanel(false);
                    } else if (step.getPasteValueType() == Step.PasteValueType.DYNAMIC && radio.getText().equals("动态值")) {
                        radio.setSelected(true);
                        dynamicNameField.setText(step.getPasteDynamicName());
                        showFixedValuePanel(false);
                        showDynamicValuePanel(true);
                    }
                }
            }
        }

        waitTimeSpinner.setValue(step.getWaitTime());

        setSize(600, 720);  // 进一步增加高度
        setLocationRelativeTo(parent);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }

    /**
     * 初始化界面组件
     * <p>
     * 创建并布局主要的界面组件：
     * 1. 主面板：使用BorderLayout布局
     * 2. 标题面板：包含渐变背景和标题文本
     * 3. 内容面板：包含步骤设置的各个部分
     * 4. 按钮面板：包含确定和取消按钮
     */
    private void initComponents() {
        // 设置窗口风格
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 1. 创建主面板，带白色背景
        JPanel mainPanel = StepDialogUIUtil.createMainPanel();

        // 2. 创建标题面板，带渐变背景
        JPanel titlePanel = StepDialogUIUtil.createTitlePanel(600, 60, HEADER_GRADIENT_START, HEADER_GRADIENT_END, "步骤设置");
        mainPanel.add(titlePanel, BorderLayout.NORTH);

        // 3. 创建内容面板，纵向布局
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
        contentPanel.setBackground(PANEL_BACKGROUND);
        contentPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 步骤名称面板
        JPanel namePanel = new JPanel(new BorderLayout(10, 0));
        namePanel.setBackground(PANEL_BACKGROUND);
        namePanel.setBorder(StepDialogUIUtil.createSectionBorder("基本信息", BORDER_COLOR, HEADER_GRADIENT_START));

        JLabel nameLabel = new JLabel("步骤名称:");
        nameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        namePanel.add(nameLabel, BorderLayout.WEST);

        nameField = new JTextField(30);
        nameField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        nameField.setPreferredSize(new Dimension(300, 35));
        nameField.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(BORDER_COLOR),
                BorderFactory.createEmptyBorder(5, 7, 5, 7)));
        namePanel.add(nameField, BorderLayout.CENTER);

        contentPanel.add(namePanel);
        contentPanel.add(Box.createRigidArea(new Dimension(0, 15)));

        // 操作类型面板
        JPanel actionTypePanel = new JPanel();
        actionTypePanel.setLayout(new BoxLayout(actionTypePanel, BoxLayout.Y_AXIS));
        actionTypePanel.setBackground(PANEL_BACKGROUND);
        actionTypePanel.setBorder(StepDialogUIUtil.createSectionBorder("操作类型", BORDER_COLOR, HEADER_GRADIENT_START));

        // 单选按钮组
        ButtonGroup group = new ButtonGroup();

        // 快捷键选项面板
        JPanel shortcutOptionPanel = new JPanel(new BorderLayout());
        shortcutOptionPanel.setBackground(PANEL_BACKGROUND);
        shortcutOptionPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 40));

        shortcutRadio = StepDialogUIUtil.createStyledRadioButton("使用快捷键", PANEL_BACKGROUND);
        shortcutRadio.addActionListener(e -> {
            enableShortcutPanel(true);
            enableCoordinatePanel(false);
            enablePastePanel(false);
        });
        group.add(shortcutRadio);
        shortcutOptionPanel.add(shortcutRadio, BorderLayout.WEST);
        actionTypePanel.add(shortcutOptionPanel);

        // 快捷键面板
        shortcutPanel = new JPanel(new BorderLayout(10, 0));
        shortcutPanel.setBackground(PANEL_BACKGROUND);
        shortcutPanel.setBorder(BorderFactory.createEmptyBorder(10, 30, 10, 10));

        JLabel shortcutLabel = new JLabel("快捷键:");
        shortcutLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        shortcutPanel.add(shortcutLabel, BorderLayout.WEST);

        JPanel shortcutInputPanel = new JPanel(new BorderLayout(10, 0));
        shortcutInputPanel.setBackground(PANEL_BACKGROUND);

        shortcutField = new JTextField(20);
        shortcutField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        shortcutField.setPreferredSize(new Dimension(200, 35));
        shortcutField.setMaximumSize(new Dimension(250, 35)); // 限制最大宽度
        shortcutField.setEditable(false); // 设置为不可编辑，必须通过录制录入
        shortcutField.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(BORDER_COLOR),
                BorderFactory.createEmptyBorder(5, 7, 5, 7)));
        shortcutInputPanel.add(shortcutField, BorderLayout.CENTER);

        recordShortcutButton = StepDialogUIUtil.createStyledButton("录制快捷键", BUTTON_BACKGROUND, BUTTON_HOVER, BUTTON_TEXT);
        recordShortcutButton.setPreferredSize(new Dimension(120, 35)); // 固定按钮宽度
        recordShortcutButton.addActionListener(e -> recordShortcut());
        shortcutInputPanel.add(recordShortcutButton, BorderLayout.EAST);

        shortcutPanel.add(shortcutInputPanel, BorderLayout.CENTER);
        actionTypePanel.add(shortcutPanel);
        actionTypePanel.add(Box.createRigidArea(new Dimension(0, 10)));

        // 坐标选项面板
        JPanel coordinateOptionPanel = new JPanel(new BorderLayout());
        coordinateOptionPanel.setBackground(PANEL_BACKGROUND);
        coordinateOptionPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 40));

        coordinateRadio = StepDialogUIUtil.createStyledRadioButton("使用坐标", PANEL_BACKGROUND);
        coordinateRadio.addActionListener(e -> {
            enableShortcutPanel(false);
            enableCoordinatePanel(true);
            enablePastePanel(false);
        });
        group.add(coordinateRadio);
        coordinateOptionPanel.add(coordinateRadio, BorderLayout.WEST);
        actionTypePanel.add(coordinateOptionPanel);

        // 坐标面板
        coordinatePanel = new JPanel();
        coordinatePanel.setLayout(new BoxLayout(coordinatePanel, BoxLayout.Y_AXIS));
        coordinatePanel.setBackground(PANEL_BACKGROUND);
        coordinatePanel.setBorder(BorderFactory.createEmptyBorder(10, 30, 10, 10));

        // 坐标输入面板
        JPanel coordInputPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 5));
        coordInputPanel.setBackground(PANEL_BACKGROUND);

        JLabel xLabel = new JLabel("X坐标:");
        xLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        coordInputPanel.add(xLabel);

        xSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 3000, 1));
        xSpinner.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        xSpinner.setPreferredSize(new Dimension(100, 35));
        JSpinner.NumberEditor xEditor = new JSpinner.NumberEditor(xSpinner, "#");
        xSpinner.setEditor(xEditor);
        coordInputPanel.add(xSpinner);

        JLabel yLabel = new JLabel("Y坐标:");
        yLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        coordInputPanel.add(yLabel);

        ySpinner = new JSpinner(new SpinnerNumberModel(0, 0, 3000, 1));
        ySpinner.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        ySpinner.setPreferredSize(new Dimension(100, 35));
        JSpinner.NumberEditor yEditor = new JSpinner.NumberEditor(ySpinner, "#");
        ySpinner.setEditor(yEditor);
        coordInputPanel.add(ySpinner);

        coordinatePanel.add(coordInputPanel);

        // 点击类型面板
        JPanel clickTypePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 5));
        clickTypePanel.setBackground(PANEL_BACKGROUND);

        JLabel clickTypeLabel = new JLabel("点击类型:");
        clickTypeLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        clickTypePanel.add(clickTypeLabel);

        ButtonGroup clickTypeGroup = new ButtonGroup();
        singleClickRadio = StepDialogUIUtil.createStyledRadioButton("单击", PANEL_BACKGROUND);
        singleClickRadio.setSelected(true);
        doubleClickRadio = StepDialogUIUtil.createStyledRadioButton("双击", PANEL_BACKGROUND);
        clickTypeGroup.add(singleClickRadio);
        clickTypeGroup.add(doubleClickRadio);

        clickTypePanel.add(singleClickRadio);
        clickTypePanel.add(doubleClickRadio);

        coordinatePanel.add(clickTypePanel);

        // 坐标按钮面板
        JPanel coordButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
        coordButtonPanel.setBackground(PANEL_BACKGROUND);

        JButton captureBtn = StepDialogUIUtil.createStyledButton("捕获坐标", BUTTON_BACKGROUND, BUTTON_HOVER, BUTTON_TEXT);
        captureBtn.addActionListener(e -> captureCoordinate());

        JButton testBtn = StepDialogUIUtil.createStyledButton("测试坐标", BUTTON_BACKGROUND, BUTTON_HOVER, BUTTON_TEXT);
        testBtn.addActionListener(e -> testCoordinate());

        coordButtonPanel.add(captureBtn);
        coordButtonPanel.add(testBtn);

        coordinatePanel.add(coordButtonPanel);
        actionTypePanel.add(coordinatePanel);
        actionTypePanel.add(Box.createRigidArea(new Dimension(0, 10)));

        // 粘贴选项面板
        JPanel pasteOptionPanel = new JPanel(new BorderLayout());
        pasteOptionPanel.setBackground(PANEL_BACKGROUND);
        pasteOptionPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 40));

        pasteRadio = StepDialogUIUtil.createStyledRadioButton("使用复制粘贴", PANEL_BACKGROUND);
        pasteRadio.addActionListener(e -> {
            enableShortcutPanel(false);
            enableCoordinatePanel(false);
            enablePastePanel(true);
        });
        group.add(pasteRadio);
        pasteOptionPanel.add(pasteRadio, BorderLayout.WEST);
        actionTypePanel.add(pasteOptionPanel);

        // 粘贴面板
        pastePanel = new JPanel();
        pastePanel.setLayout(new BoxLayout(pastePanel, BoxLayout.Y_AXIS));
        pastePanel.setBackground(PANEL_BACKGROUND);
        pastePanel.setBorder(BorderFactory.createEmptyBorder(10, 30, 10, 10));

        // 粘贴类型面板
        JPanel pasteTypePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 5));
        pasteTypePanel.setBackground(PANEL_BACKGROUND);

        JLabel pasteTypeLabel = new JLabel("值类型:");
        pasteTypeLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        pasteTypePanel.add(pasteTypeLabel);

        // 创建单选按钮组
        ButtonGroup valueTypeGroup = new ButtonGroup();
        JRadioButton fixedValueRadio = StepDialogUIUtil.createStyledRadioButton("固定值", PANEL_BACKGROUND);
        JRadioButton dynamicValueRadio = StepDialogUIUtil.createStyledRadioButton("动态值", PANEL_BACKGROUND);

        fixedValueRadio.setSelected(true); // 默认选中固定值
        fixedValueRadio.addActionListener(e -> {
            showFixedValuePanel(true);
            showDynamicValuePanel(false);
        });

        dynamicValueRadio.addActionListener(e -> {
            showFixedValuePanel(false);
            showDynamicValuePanel(true);
        });

        valueTypeGroup.add(fixedValueRadio);
        valueTypeGroup.add(dynamicValueRadio);

        pasteTypePanel.add(fixedValueRadio);
        pasteTypePanel.add(dynamicValueRadio);

        pastePanel.add(pasteTypePanel);

        // 固定值面板
        fixedValuePanel = new JPanel(new BorderLayout(5, 5));
        fixedValuePanel.setBackground(PANEL_BACKGROUND);
        fixedValuePanel.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(BORDER_COLOR),
                "固定值",
                TitledBorder.LEFT,
                TitledBorder.TOP,
                new Font("微软雅黑", Font.BOLD, 14),
                HEADER_GRADIENT_START));

        pasteValueField = new JTextArea(4, 30);
        pasteValueField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        pasteValueField.setLineWrap(true);
        pasteValueField.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(pasteValueField);
        scrollPane.setBorder(BorderFactory.createLineBorder(BORDER_COLOR));
        fixedValuePanel.add(scrollPane, BorderLayout.CENTER);

        // 动态值面板
        dynamicValuePanel = new JPanel(new BorderLayout(5, 5));
        dynamicValuePanel.setBackground(PANEL_BACKGROUND);
        dynamicValuePanel.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(BORDER_COLOR),
                "动态值名称",
                TitledBorder.LEFT,
                TitledBorder.TOP,
                new Font("微软雅黑", Font.BOLD, 14),
                HEADER_GRADIENT_START));

        JPanel nameFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        nameFieldPanel.setBackground(PANEL_BACKGROUND);

        JLabel dynamicNameLabel = new JLabel("名称:");
        dynamicNameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        nameFieldPanel.add(dynamicNameLabel);

        dynamicNameField = new JTextField(20);
        dynamicNameField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        dynamicNameField.setPreferredSize(new Dimension(200, 35));
        dynamicNameField.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(BORDER_COLOR),
                BorderFactory.createEmptyBorder(5, 7, 5, 7)));
        nameFieldPanel.add(dynamicNameField);

        dynamicValuePanel.add(nameFieldPanel, BorderLayout.CENTER);

        pastePanel.add(fixedValuePanel);
        pastePanel.add(dynamicValuePanel);
        actionTypePanel.add(pastePanel);

        contentPanel.add(actionTypePanel);
        contentPanel.add(Box.createRigidArea(new Dimension(0, 15)));

        // 等待时间面板
        JPanel waitTimePanel = new JPanel(new BorderLayout(10, 0));
        waitTimePanel.setBackground(PANEL_BACKGROUND);
        waitTimePanel.setBorder(StepDialogUIUtil.createSectionBorder("等待时间", BORDER_COLOR, HEADER_GRADIENT_START));
        waitTimePanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 100));

        JLabel waitTimeLabel = new JLabel("执行后等待时间(秒):");
        waitTimeLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        waitTimePanel.add(waitTimeLabel, BorderLayout.WEST);

        waitTimeSpinner = new JSpinner(new SpinnerNumberModel(1, 0, 60, 1));
        waitTimeSpinner.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        waitTimeSpinner.setPreferredSize(new Dimension(100, 35));
        JSpinner.NumberEditor editor = new JSpinner.NumberEditor(waitTimeSpinner, "#");
        waitTimeSpinner.setEditor(editor);

        JPanel spinnerWrapPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
        spinnerWrapPanel.setBackground(PANEL_BACKGROUND);
        spinnerWrapPanel.add(waitTimeSpinner);
        waitTimePanel.add(spinnerWrapPanel, BorderLayout.CENTER);

        contentPanel.add(waitTimePanel);

        // 4. 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
        buttonPanel.setBackground(Color.WHITE);
        buttonPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, BORDER_COLOR));

        JButton cancelButton = StepDialogUIUtil.createStyledButton("取消", BUTTON_BACKGROUND, BUTTON_HOVER, BUTTON_TEXT);
        cancelButton.addActionListener(e -> dispose());

        JButton saveButton = StepDialogUIUtil.createStyledButton("确定", BUTTON_BACKGROUND, BUTTON_HOVER, BUTTON_TEXT);
        saveButton.addActionListener(e -> {
            updateCurrentStep();
            confirmed = true;
            dispose();
        });

        buttonPanel.add(cancelButton);
        buttonPanel.add(saveButton);

        // 添加到主面板
        JScrollPane contentScrollPane = new JScrollPane(contentPanel);
        contentScrollPane.setBorder(null);
        contentScrollPane.getVerticalScrollBar().setUnitIncrement(16);
        mainPanel.add(contentScrollPane, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        // 设置主面板
        setContentPane(mainPanel);

        // 默认选中快捷键模式，并设置面板可见性
        shortcutPanel.setVisible(true);
        coordinatePanel.setVisible(false);
        pastePanel.setVisible(false);

        shortcutRadio.setSelected(true);
        enableShortcutPanel(true);
        enableCoordinatePanel(false);
        enablePastePanel(false);
    }

    /**
     * 启用/禁用快捷键面板
     * <p>
     * 控制快捷键相关组件的启用状态，包括：
     * - 快捷键输入框
     * - 录制快捷键按钮
     *
     * @param enable true表示启用，false表示禁用
     */
    private void enableShortcutPanel(boolean enable) {
        // 设置面板可见性和组件启用状态
        shortcutPanel.setVisible(enable);
        shortcutPanel.setEnabled(enable);
        shortcutField.setEnabled(enable);
        recordShortcutButton.setEnabled(enable);
    }

    /**
     * 启用/禁用坐标面板
     * <p>
     * 控制坐标面板的可见性和组件的启用状态，包括：
     * - 坐标输入框
     * - 点击类型选择
     * - 坐标操作按钮
     *
     * @param enable true表示启用，false表示禁用
     */
    private void enableCoordinatePanel(boolean enable) {
        // 设置面板可见性
        coordinatePanel.setVisible(enable);
        coordinatePanel.setEnabled(enable);

        // 设置组件启用状态
        xSpinner.setEnabled(enable);
        ySpinner.setEnabled(enable);
        singleClickRadio.setEnabled(enable);
        doubleClickRadio.setEnabled(enable);

        // 启用/禁用所有按钮
        for (Component c : coordinatePanel.getComponents()) {
            if (c instanceof JButton) {
                c.setEnabled(enable);
            }
        }
    }

    /**
     * 启用/禁用粘贴面板
     * <p>
     * 控制粘贴面板的可见性和组件的启用状态，包括：
     * - 值类型选择
     * - 固定值输入区域
     * - 动态值名称输入框
     *
     * @param enable true表示启用，false表示禁用
     */
    private void enablePastePanel(boolean enable) {
        // 设置面板可见性
        pastePanel.setVisible(enable);
        pastePanel.setEnabled(enable);

        // 启用/禁用所有单选按钮
        Component[] components = ((JPanel) pastePanel.getComponent(0)).getComponents();
        for (Component c : components) {
            if (c instanceof JRadioButton) {
                c.setEnabled(enable);
            }
        }

        // 启用/禁用固定值面板
        pasteValueField.setEnabled(enable);

        // 启用/禁用动态值面板
        dynamicNameField.setEnabled(enable);

        // 根据当前所选的值类型设置对应面板的可见性
        if (enable) {
            JPanel pasteTypePanel = (JPanel) pastePanel.getComponent(0);
            boolean foundSelected = false;

            for (Component c : pasteTypePanel.getComponents()) {
                if (c instanceof JRadioButton) {
                    JRadioButton radio = (JRadioButton) c;
                    if (radio.isSelected()) {
                        if (radio.getText().equals("固定值")) {
                            showFixedValuePanel(true);
                            showDynamicValuePanel(false);
                        } else {
                            showFixedValuePanel(false);
                            showDynamicValuePanel(true);
                        }
                        foundSelected = true;
                        break;
                    }
                }
            }

            // 如果没有找到选中的按钮，默认选中固定值
            if (!foundSelected) {
                showFixedValuePanel(true);
                showDynamicValuePanel(false);
            }
        }
    }

    /**
     * 显示/隐藏固定值面板
     *
     * @param show true表示显示，false表示隐藏
     */
    private void showFixedValuePanel(boolean show) {
        fixedValuePanel.setVisible(show);
        if (show) {
            pasteValueField.requestFocus();
        }
    }

    /**
     * 显示/隐藏动态值面板
     *
     * @param show true表示显示，false表示隐藏
     */
    private void showDynamicValuePanel(boolean show) {
        dynamicValuePanel.setVisible(show);
        if (show) {
            dynamicNameField.requestFocus();
        }
    }

    /**
     * 更新当前步骤对象
     * <p>
     * 根据界面上的设置更新步骤对象的属性：
     * 1. 基本信息：名称和等待时间
     * 2. 根据选择的操作类型设置相应的属性：
     * - 快捷键：设置快捷键组合
     * - 坐标：设置坐标点和点击类型
     * - 粘贴：设置值类型和具体的值
     */
    private void updateCurrentStep() {
        // 更新基本信息
        currentStep.setName(nameField.getText().trim());
        currentStep.setWaitTime((Integer) waitTimeSpinner.getValue());

        // 根据选择的操作类型更新相应的属性
        if (shortcutRadio.isSelected()) {
            currentStep.setActionType(Step.ActionType.SHORTCUT);
            currentStep.setShortcut(shortcutField.getText());
        } else if (coordinateRadio.isSelected()) {
            currentStep.setActionType(Step.ActionType.COORDINATE);
            currentStep.setCoordinate(new Point(
                    (Integer) xSpinner.getValue(),
                    (Integer) ySpinner.getValue()));
            currentStep.setClickType(doubleClickRadio.isSelected() ? Step.ClickType.DOUBLE : Step.ClickType.SINGLE);
        } else if (pasteRadio.isSelected()) {
            currentStep.setActionType(Step.ActionType.PASTE);

            // 获取值类型选择
            JPanel pasteTypePanel = (JPanel) pastePanel.getComponent(0);
            Component[] components = pasteTypePanel.getComponents();
            for (Component c : components) {
                if (c instanceof JRadioButton) {
                    JRadioButton radio = (JRadioButton) c;
                    if (radio.isSelected()) {
                        if (radio.getText().equals("固定值")) {
                            currentStep.setPasteValueType(Step.PasteValueType.FIXED);
                            currentStep.setPasteFixedValue(pasteValueField.getText());
                        } else {
                            currentStep.setPasteValueType(Step.PasteValueType.DYNAMIC);
                            currentStep.setPasteDynamicName(dynamicNameField.getText().trim());
                        }
                        break;
                    }
                }
            }
        }
    }

    /**
     * 获取当前编辑的步骤对象
     *
     * @return 步骤对象
     */
    public Step getStep() {
        return currentStep;
    }

    /**
     * 捕获鼠标坐标
     * <p>
     * 打开坐标捕获对话框，允许用户捕获屏幕上的鼠标坐标。
     * 捕获成功后，更新坐标输入框的显示。
     */
    private void captureCoordinate() {
        CaptureCoordsDialog dialog = new CaptureCoordsDialog(this);
        // 使用对话框管理器显示对话框
        DialogManager.getInstance().showDialog(dialog, this);

        if (dialog.isCoordinateCaptured()) {
            Point p = dialog.getCapturedPoint();
            if (p != null) {
                xSpinner.setValue(p.x);
                ySpinner.setValue(p.y);
            }
        }
    }

    /**
     * 测试坐标
     * <p>
     * 使用MouseActionUtil工具类将鼠标移动到设置的坐标位置，
     * 并根据当前选择的点击类型执行单击或双击操作，
     * 帮助用户确认坐标和点击效果是否正确。
     */
    private void testCoordinate() {
        try {
            Point point = new Point(
                    (Integer) xSpinner.getValue(),
                    (Integer) ySpinner.getValue());

            // 根据当前选择的点击类型执行相应的操作
            boolean isDoubleClick = doubleClickRadio.isSelected();

            // 使用MouseActionUtil执行点击操作
            if (MouseActionUtil.click(point.x, point.y, isDoubleClick)) {
                JOptionPane.showMessageDialog(this,
                        "鼠标已移动到设置的坐标位置并执行" + (isDoubleClick ? "双击" : "单击") + "操作。",
                        "测试坐标",
                        JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(this,
                        "坐标操作失败，请检查系统权限。",
                        "错误",
                        JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this,
                    "执行坐标测试失败：" + e.getMessage(),
                    "错误",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
     * 录制快捷键
     * <p>
     * 打开快捷键录制对话框，允许用户录制键盘快捷键。
     * 录制成功后，更新快捷键输入框的显示。
     */
    private void recordShortcut() {
        ShortcutRecorderDialog dialog = new ShortcutRecorderDialog(this);
        // 使用对话框管理器显示对话框
        DialogManager.getInstance().showDialog(dialog, this);

        if (dialog.isShortcutRecorded()) {
            String shortcut = dialog.getRecordedShortcut();
            shortcutField.setText(shortcut);
        }
    }
}
